<?php

namespace app\wxapp\controller;

use app\lib\MiniProgramAccount;
use app\model\Comm;
use app\model\Common;
use app\model\Fans;
use app\model\Game;
use app\model\Koi;
use app\model\Lottery;
use app\model\Payment as PaymentModel;
use app\model\Points;
use app\model\Poster;
use app\model\Record;
use app\model\Relation;
use app\model\Scan;
use app\model\Theme;
use app\model\Userdata;
use app\model\Worker;
use think\Db;

class Base
{
    public function __construct()
    {
        global $_G, $_P;


        // 接收前端传入的sessionid并重置当前会话sessionid为前端传入的数据
        $sessionData = $_P['state'];
        if ($sessionData) {
            $sessionData = str_replace('we7sid-', '', $sessionData);
            $sessionid = str_replace('sid-', '', $sessionData);
            session_id($sessionid);
        } else if (\think\Request::instance()->action() !== 'oauth' && ($_P['scene'] != 1155 && $_P['scene'] != 1154)) {
            exit(json_encode(['errno' => 41009, 'message' => '请登录1', 'data' => []]));
        }

        // 根据当前的sessionid获取用户信息
        $openid = session('openid');
        if (!empty($openid)) {
            $member_res = $this->checkMemberInfo($openid);
            if (empty($member_res) && ($_P['scene'] != 1155 && $_P['scene'] != 1154)) {
                exit(json_encode(['errno' => 41009, 'message' => '请登录', 'data' => []]));
            }
        }
    }


    protected function checkMemberInfo($openid)
    {
        global $_G;
        //查询member信息是否存在
        $member_res = member_info($openid);
        if (empty($member_res)) {
            return [];
        }
        $member_res['is_auth'] = 0;
        unset($member_res['unionid']);
        if ($member_res['nickname'] != '微信用户' && $member_res['avatar'] != 'http://css2020.yixuan.net/2020css/yx2020/images/question.png') {
            $member_res['is_auth'] = 1;
        }
        $_G['openid'] = $openid;
        //挂载全局member
        $_G['member'] = $member_res;
        //挂载全局uid
        $_G['uid'] = $member_res['uid'];
        return $member_res;
    }


    protected function checkuser($member = "")
    {
        global $_P, $_G;
        $_G['member']['uid'] = Fans::getRealUid();
        $uid = intval($_G['member']['uid']);
        if ($uid < 1) {
//         wxapp_result(1, '用户信息出错');
//         exit;
            return;
        }
//    if ($uid =='630'){
//        $_G['member']['uid']=1566668;
//        $_G['openid']='oK7mf0QOwLjjEV1R6MLqOknYhrN0';
//    }

        $system = $_G['system'];
        if ($_G['mpid'] != $system['wxapp'] && $system['wxapp_demo'] == 0 && DOMAIN0 != "30.yixuan.net" && $system['wxapp_hutong'] != 1) {
            wxapp_result(1, '活动入口不正确：小程序' . MODULE_NAME . '与后台配置不一致，请联系客服' . $_G['mpid'] . ":" . $system['wxapp']);
            exit;
        }

        $themeid = intval($_P['themeid']);
        $theme = Theme::getThemeByIdxcx($themeid);
        $config = Theme::getConfig($themeid);
        if (empty($theme['id'])) {
            wxapp_result(1, '活动入口不正确');
            exit;
        }

        $fromuid = intval($_P['fromuid']);
        if (!empty($theme['blacklist'])) {
            if (!empty(intval($_P['fromuid']))) {
                if (strstr($theme['blacklist'] . "|", "|" . $_P['fromuid'] . "|") !== false) {
                    $txt = !empty($theme['mobile']) ? '<br><br>咨询电话：' . $theme['mobile'] : '';
                    echo '<title>活动暂停</title><H1>活动暂停，给您带来的不便还望凉解；' . $txt . '</H1>';
                    exit;
                }
            }
        }

        $workerid = intval($_P['workerid']);

        $isworker = Worker::getThemeWorkerByUid($fromuid, $themeid, 1);
        if (!empty($isworker)) {
            $workerid = $isworker['uid'];
        }
        if ($fromuid == $uid) {
            $fromuid = 0;
            $fopenid = "";
        } elseif ($workerid > 0 && $fromuid == 0) {
            $isworker = Worker::getThemeWorkerByUid($workerid, $themeid, 1);
            $fromuid = $isworker['uid'];
            $fopenid = $isworker['openid'];
        } elseif ($fromuid > 0) {
            $pm = array(":themeid" => $themeid, ":uid" => $fromuid);
            $fromfans = db_find("SELECT * FROM " . tablename(t_fans) . "
WHERE themeid = :themeid and uid = :uid", $pm);
            $fopenid = $fromfans['openid'];
        }

        if (empty($member)) {
            if (!empty($_G['openid'])) {
                $member = member_info($_G['openid']);
            }
        }

//    yxlog($member,"wxapp-member");

        $avatar = empty($member['avatar']) ? $member['headimgurl'] : $member['avatar'];
        $nickname = stripcslashes(filterEmoji($member['nickname']));
// var_dump($member);
// var_dump($_G['member']);
// echo ($_G['member']['username']);
// echo ($_G['member']['mobile']);
        $avatar_default = 'https://css2020.yixuan.net/2020css/yx2020/images/question.png';
        $pm = array(":themeid" => $themeid, ":openid" => $_G['openid']);
        $fans = db_find("SELECT * FROM " . tablename(t_fans) . "
WHERE themeid = :themeid and openid = :openid", $pm);
        if (!empty($fans)) {
            if ($fans['status'] == 5 && !empty($_G['member']['uid'])) {
//如果粉丝状态为链接封杀，则更新uid
                db_update(t_fans, array('status' => '1', 'uid' => $_G['member']['uid']), array('uid' => $fans['uid']));
                Fans::updateAlluid($fans['uid'], $_G['member']['uid']);
                $fans['status'] = 1;
                $fans['uid'] = $_G['member']['uid'];
            }
        }
        if ($_P['recordid'] == 999997 && $_P['fromuid'] == intval(substr(MODULE_NAME, -5))) {
            Fans::addworker($theme, $themeid, $_G['member']['uid']);
        }
//小程序授权手机号
        if (!empty($fans) && empty($fans['mobile']) && empty($_G['member']['mobile'])) {
            $member_t = db_find("SELECT mobile FROM " . tablename('yx_sys_members') . " WHERE uid = '" . $fans['uid'] . "'");
            if ($member_t['mobile']) {
                $_G['member']['mobile'] = $member_t['mobile'];
            }
            unset($member_t);
        }

        if (empty($fans)) {
//load_model('mc');

            $postdata = array(
                'uid' => $_G['member']['uid'],
                'nickname' => stripcslashes(filterEmoji($member['nickname'])),
                'avatar' => $avatar,
                'fromuid' => $fromuid,
                'ip' => $_G['clientip'],
            );
            $system['themeid'] = $themeid;
            $theme = Theme::getById($themeid);
            $status = Poster::GetblacklistWxapp($system, $config, $postdata, $theme['blacklist'], $theme['whitelist']);

// var_dump($system);
// var_dump($postdata);
// var_dump($theme['blacklist']);
// var_dump($theme['whitelist']);
// echo  $status;
            $data = array(
                'themeid' => $themeid,
                'uniacid' => $_G['mpid'],
                'uid' => $_G['member']['uid'],
                'openid' => $_G['openid'],
                'username' => '',
                'mobile' => $_G['member']['mobile'],
                'nickname' => empty($nickname) ? '新用户' : $nickname,
                'avatar' => empty($avatar) ? $avatar_default : $avatar,
                'status' => $status,
                'fromuid' => $fromuid,
                'fopenid' => $fopenid,
                'ip' => $_G['clientip'],
                'ipaddress' => $_G['address']['province'] . $_G['address']['city'],
                'workerid' => $workerid,
                'addtime' => TIMESTAMP,
            );
            $pm = array(":themeid" => $themeid, ":openid" => $_G['openid']);
            $fans = db_find("SELECT * FROM " . tablename(t_fans) . "
WHERE themeid = :themeid and openid = :openid", $pm);
            if (!empty($fans)) {
                return $fans;
            }

            $insertid = db_insert(t_fans, $data);

            $p = array(":themeid" => $themeid);
            db_query("UPDATE " . tablename(t_theme) . " SET fanscount = fanscount +1 WHERE id =:themeid", $p);

            $p = array(":themeid" => $themeid);
            db_query("UPDATE " . tablename(t_report) . " SET newfans = newfans +1 WHERE software = '" . MODULE_NAME . "' and themeid =:themeid and riqi ='" . date("Y-m-d") . "'", $p);
            Theme::workerreport($themeid, $workerid, array('newfans' => 1));
            $fans = $data;

//宝妈拼拼看
            if ($system['is_lz_game'] == 1 && $_G['member']['uid'] != $fromuid && !empty($fromuid)) {
                if (!$config['game_on_off']) {
                    $config2 = Theme::getConfig($config['lz_game_themeid']);
                    if ($config2['game_guest_limit'] == 0) {
                        Game::addUserGameLifeFlag($config['lz_game_themeid'], $_G['member']['uid'], $fromuid, 'friend');
                    }
                } elseif ($config['game_guest_limit'] == 0) {
                    Game::addUserGameLifeFlag($themeid, $_G['member']['uid'], $fromuid, 'friend');
                }
            }


//转盘抽奖增加抽奖次数及积分
            if ($system['ispoints'] == 1 && $config['pointsviewtype'] == 1) {
//self_look 未报名 self_pay 自己下单 friend_look 好友到访 friend_pay 好友下单 share_moments 朋友圈 share_qun 群
                if (!empty($fromuid) && $fromuid != $fans['uid']) {
                    $fans = Fans::getById($fromuid, $themeid);
                    $fans['avatar'] = $avatar;
                    Points::changeLotteryOrPoints(1, 'friend_look', false, $themeid, $system, $fans);
                }//调用领奖后操作
                Lottery::opReceiveRemainNum('new_friend', $fans, 0);
            }

//限制地区访问
            if (!empty($config['viewcity']) && $fans['status'] == 1) {
                $ClientIp = $_G['clientip'];
                $viewresult = Comm::getCity($system, $_G['clientip'], $config['viewcity']);
                if ($viewresult['status'] == 0) {
                    $whitestate = 0;
                    if (!empty($config['viewcitywhitelist'])) {
                        $softwhitelist = str_replace(",", "|", $config['viewcitywhitelist']);
                        $nickname = $fans['nickname'];
                        $whitelist = explode("|", "|" . $softwhitelist);

                        foreach ($whitelist as $key => $row) {
                            if (!empty($row)) {
                                if (strstr($nickname, $row) !== false) {
                                    $whitestate = 1;
                                }
                            }
                        }
                    }
                    if ($whitestate == 0) {
                        db_query("UPDATE " . tablename(t_fans) . " SET status = 0 WHERE uid = '" . $_G['member']['uid'] . "' and status <> '2'");
                        $fans['status'] = 0;
                    }
                }
            }
            if ($fans['nickname'] != '微信用户' && $fans['nickname'] != '新用户' || $config['comm5UserLimit'] == 1) {
                Common::newfans($system, $theme, $config, $themeid, $status, $fans);
            }

        } else {
            if (($fans['nickname'] == '微信用户' || $fans['nickname'] == '新用户') && !empty($nickname) && $nickname != '微信用户') {
                $update_nickname = true;
            } else {
                $update_nickname = false;
            }
            if ((($fans['avatar'] == $avatar_default || empty($fans['avatar'])) && !empty($avatar))
                || (empty($fans['mobile']) && !empty($_G['member']['mobile']))
                || $update_nickname
            ) {
                $data = array();
//        echo 'a';
//        exit;
//            if((($fans['avatar']==$avatar_default || empty($fans['avatar'])) && !empty($avatar)) || (($fans['nickname']=='新用户' || $fans['nickname']=='微信用户') && !empty($nickname))) {
//                $data['nickname']    = $nickname;
//                $data['avatar']    = $avatar;
//                db_update(t_scan,$data,array("uid"=>$uid));
//                db_update(t_friend,$data,array("uid"=>$uid));
//                db_update(t_relation,$data,array("uid"=>$uid));
//            }

//            if ((($fans['avatar'] == $avatar_default || empty($fans['avatar'])) && !empty($avatar)) || $update_nickname) {
                if ((($fans['avatar'] == $avatar_default || empty($fans['avatar'])) && !empty($avatar) && $avatar != $avatar_default) || $update_nickname) {
                    $data['nickname'] = $nickname;
                    $data['avatar'] = $avatar;
                    db_update(t_scan, $data, array("uid" => $uid));
                    db_update(t_friend, $data, array("uid" => $uid));
                    db_update(t_relation, $data, array("uid" => $uid));
                    $fans['nickname'] = $nickname;
                    if ($update_nickname) {
                        Common::newfans($system, $theme, $config, $themeid, $fans['status'], $fans);
                    }
                    userdata::addLog($themeid, $_G['member']['uid'], 38);
                }
                if (empty($fans['mobile']) && !empty($_G['member']['mobile']) && $_G['member']['mobile'] != 'undefined') {
// $data['username']    = $_G['member']['username'];
                    $data['mobile'] = $_G['member']['mobile'];
                }
                if (!empty($data)) {
                    member_update($uid, $data);
                    db_update(t_fans, $data, array("uid" => $uid));
                    db_update(t_worker, $data, array("uid" => $uid));
                    db_update(t_record, $data, array("uid" => $uid));
                    if ($system['fanpai'] == 1) {
                        unset($data['mobile']);
                        db_update(t_fanpaijihui, $data, array("uid" => $uid));
                        db_update(t_fanpaichoujiang, $data, array("uid" => $uid));
                    }
                }

                $fans = db_find("SELECT * FROM " . tablename(t_fans) . "
WHERE themeid = :themeid and openid = :openid", $pm);
            } else {
//宝妈拼拼看-所有用户
                if ($system['is_lz_game'] == 1 && $_G['member']['uid'] != $fromuid && !empty($fromuid)) {
                    if (!$config['game_on_off']) {
                        $config2 = Theme::getConfig($config['lz_game_themeid']);
                        if ($config2['game_guest_limit'] == 1) {
                            Game::addUserGameLifeFlag($config['lz_game_themeid'], $_G['member']['uid'], $fromuid, 'friend');
                        }
                    } elseif ($config['game_guest_limit'] == 1) {
                        Game::addUserGameLifeFlag($themeid, $_G['member']['uid'], $fromuid, 'friend');
                    }
                }

//检查员第二次访问
                $wxname = explode(",", $system['s']['wxname1'] . "," . $system['s']['wxname2']);
                if ($_G['ISweixinip'] !== false || in_array($member['nickname'], $wxname) || strpos($member['nickname'], "rdgztest") !== false) {
                    $postdata = array(
                        'uid' => $_G['member']['uid'],
                        'nickname' => stripcslashes(filterEmoji($member['nickname'])),
                        'avatar' => empty($member['avatar']) ? $member['headimgurl'] : $member['avatar'],
                        'fromuid' => $_P['fromuid'],
                        'ip' => $_G['clientip'],
                    );
                    $system['themeid'] = $themeid;
                    $status = Poster::GetblacklistWxapp($system, $config, $postdata, $theme['blacklist'], $theme['whitelist']);
                    if ($status != $fans['status'] && $fans['status'] != 2) {
                        db_query("UPDATE " . tablename(t_fans) . " SET status = " . $status . " WHERE uid = '" . $_G['member']['uid'] . "' and status <> '2'");
                        $fans['status'] = $status;
                    }
                }
            }
        }

//如是打开不是自个的链接,记录打开的关系,并重新打开链接
        if ($fans['uid'] != $fromuid) {
            if ($config['isicon'] == 3) {
                if (!empty($_P['help_uid'])) {
                    $help_uid = $_P['help_uid'];
                } else {
                    $help_uid = $_P['fromuid'];
                }
            }

            if ($fromuid > 0 && !empty($fopenid)) {
//判断推荐者是不是员工
                $relation = Relation::getByThemeId($themeid, $fans['openid']);
                $data = array(
                    'themeid' => $themeid,
                    'uniacid' => $_G['mpid'],
                    'uid' => $fans['uid'],
                    'openid' => $fans['openid'],
                    'nickname' => $fans['nickname'],
                    'avatar' => $fans['avatar'],
                    'status' => 1,
                    'addtime' => TIMESTAMP,
                    'parentid' => $fromuid,
                    'popenid' => $fopenid,
                );
                if ($system['hdget'] == 1 && Fans::getuid_old($fromuid, $themeid)) {
                    $data['parentid_old'] = $fromuid;
                    $data['popenid_old'] = $fopenid;
                }
                if (empty($relation)) {
                    if ($system['hdget'] == 1 && Fans::getuid_old($workerid, $themeid)) {
                        $data['workerid_old'] = $workerid;
                    }
                    $data['workerid'] = $workerid;
                    $insertid = db_insert(t_relation, $data);
                } elseif ($relation['isorder'] != 1) {//如果没有付款则更新上下关系
                    if ($relation['popenid'] != $fopenid && $relation['isfrozen'] != 1) {
                        Relation::update($relation['id'], $data);
                    }
                    if ($relation['workerid'] != $workerid && $workerid > 0) {
                        if ($system['hdget'] == 1 && Fans::getuid_old($workerid, $themeid)) {
                            Relation::update($relation['id'], array('workerid' => $workerid, 'workerid_old' => $workerid));
                        } else {
                            Relation::update($relation['id'], array('workerid' => $workerid));
                        }
                    }
                }
            }
            $sacn = Scan::getByThemeIduid($themeid, $fans['uid']);

            if (empty($sacn)) {
                $data = array(
                    'themeid' => $themeid,
                    'uniacid' => $_G['mpid'],
                    'uid' => $fans['uid'],
                    'openid' => $fans['openid'],
                    'nickname' => $fans['nickname'],
                    'avatar' => $fans['avatar'],
                    'status' => 0,
                    'addtime' => TIMESTAMP,
                    'fromuid' => $fromuid,
                    'fopenid' => $fopenid,
                    'workerid' => $workerid,
                );
                if ($system['hdget'] == 1) {
                    if (Fans::getuid_old($fromuid, $themeid)) {
                        $data['fromuid_old'] = $fromuid;
                        $data['fopenid_old'] = $fopenid;
                    }
                    if (Fans::getuid_old($workerid, $themeid)) {
                        $data['workerid_old'] = $workerid;
                    }
                }
                if ($config['iconlimit'] > 0) {
                    $todaynum = Scan::getFpenidTodayNumXCX($themeid, $fromuid, $config['iconlimituser']);
                    if ($todaynum < $config['iconlimit']) {
                        $data['status'] = 1;
                    }
                } else {
                    $data['status'] = 1;
                }
                $insertid = db_insert(t_scan, $data);
                if (!empty($fromuid)) {
                    db_query("UPDATE " . tablename(t_fans) . " SET responsum = responsum + 1 WHERE themeid = '{$themeid}' and uid ={$fromuid}");
                }
            }
// $sharedata = array(
//     'themeid'=>$themeid,
//     'fopenid'=>$fans['openid'],
//     'fromuid'=>$fans['uid'],
//     'workerid'=>$workerid,
// );
// if (!empty($recordid)){
//     $sharedata['recordid']=$recordid;share
// }
// if ($fans['status']!=2) {
//     $workerurl =   Theme::createwxAppUrl('index',$sharedata);
//     //如果员工打开的链接为原始链接则先刷新
//     echo "<script language='javascript' type='text/javascript'>";
            //     echo "window.location.href='$workerurl';";
            //     echo "</script>";
//     exit;
// }
        } elseif ($config['isicon'] == 3) {
            if ($_P['help_uid'] != $fans['uid']) {
                if ($config['friendorder'] == 1) {
                    $help_uid = $_P['help_uid'];
                } else {
                    $sql = "SELECT * FROM " . tablename(t_record) . "
WHERE themeid =:themeid
and uid = :uid and (status > 0 or status=-2) order by id desc limit 1";
                    $isorder = db_find($sql, array(":themeid" => $themeid, ":uid" => $_P['help_uid']));

                    if (!empty($isorder)) {
                        $help_uid = $_P['help_uid'];
                    }
                }
            }
        }

        if ($fans['status'] == 1) {
            $fans['status'] = Poster::GetSoftblacklist($theme['blacklist'], $fans['nickname'], $fans['uid']);
        }
        return $fans;
    }

    protected function systemnew($system)
    {
        $systemnew = array();
        $systemnew['live'] = $system['live'];
        $systemnew['live_lottery'] = $system['live_lottery'];
        $systemnew['livetemplate'] = $system['livetemplate'];
        $systemnew['fenxiao01'] = $system['fenxiao01'];
        $systemnew['fenxiao04'] = $system['fenxiao04'];
        $systemnew['fenxiao05'] = $system['fenxiao05'];
        $systemnew['fenxiao06'] = $system['fenxiao06'];
        $systemnew['fenxiao07'] = $system['fenxiao07'];
        $systemnew['ruanjian'] = $system['ruanjian'];
        return $systemnew;
    }

//组件开关
    protected function getComponent($system, $theme, $config, $themeid, $fans)
    {
        global $_P, $_G;
        $system = Theme::getSystemByMystery($system, $config);
        $component = array(
            'ruanjian' => $system['ruanjian'],
            'iskoi' => $system['iskoi'] && Koi::checkIskoi($config) ? 1 : 0,
            'is_lz_game' => $system['is_lz_game'] && $config['game_on_off'] ? 1 : 0,
            'is_lz_comment' => $system['is_lz_comment'] && $config['comment_on_off'] ? 1 : 0,
            'is_mystery_boxes' => $system['is_mystery_boxes'] == 1 ? $config['get_mystery_boxes'] : 0,
            'is_mystery_boxes_feichi' => $system['is_mystery_boxes_feichi'] == 1 ? $config['get_mystery_boxes'] : 0,
            'is_mother_boxes' => $system['is_mother_boxes'],
            'softmenu' => $system['softmenu'] == 1 && $system['bottom_jiage'] > 0 ? 0 : $system['softmenu'],
            'is_lz_chest' => $system['is_lz_chest'],
            'is_lottery' => $system['lottery'] == 1 && TIMESTAMP > $config['yx_lt_begintime'] ? true : false,
            "is_shop_plan" => $system['is_shop_plan'],
            "ischristmas" => $system['ischristmas'],
            'yx_lt_show' => $config['yx_lt_show'],
            'yx_lt_end' => false,
            'split_redpackage' => false,//是否拆红包  默认关闭
            'redpackage_amount' => Comm::getcommssum($system, $themeid, $config['iscommshowfail']),//是否拆红包  默认关闭
            'open_danmu_btn' => $config['open_danmu_btn'],
            'redrain' => $system['redrain'],//1抵扣 2提现
            'daiyan' => $system['daiyan'],//代言
            'yyy_gifts' => $system['fenxiao071'],//摇一摇多礼品
            'yx_gift_icon' => '',//摇一摇进入我的礼品图标
            'yx_lt_mode' => 0
        );
//新版本抽奖
        $show_steer_img = '';
        if ($system['lottery'] == 1 && TIMESTAMP > $config['yx_lt_begintime'] && $config['yx_lt_mode'] == 1) {
            $component['yx_lt_mode'] = 1;

            require_once YX_PLUGIN_CORE . 'inc/plugin_model.php';
            require_once get_path('plugin/lottery/core/model.php');
//判断有无上一级
            $from_uid = intval($_P['fromuid']);
            $uid = $fans['uid'];
//无上级
//判断是否已支付
            $themyrecord = Record::getLastGroupByUid($fans['uid'], $themeid);
            if (!$themyrecord && $config['yx_lt_con'] == 0) {
                $remain_num = $fans['jihui'];
                $user_chance_flag_num = \LotteryModel::getUserChanceByUidAndThemeId($uid, $themeid);
                if ($remain_num > 0 || $user_chance_flag_num > 0) {
                    $show_steer_img = $config['yx_lt_steer_img'];
                } else {
//判断进入链接是否增加抽奖机会
                    if ($config['yx_lt_no_sign_up'] > 0) {
                        $show_steer_img = $config['yx_lt_steer_img'];
                    }
                }
            } else {
                $show_steer_img = $config['yx_lt_steer_img'];
            }
            $component['show_steer_img'] = $show_steer_img;
        }


//判断抽奖时间
        if (Common::getLotteryLimitTime($system, $config, $theme)) {
            $component['yx_lt_end'] = true;
            $component['jump_lottery'] = false;
        }
        if ($system['lottery'] == 1 && !Common::getLotteryLimitTime($system, $config, $theme)) {
//不在抽奖时间范围内
//判断当前粉丝是否存在中奖记录

            require_once YX_PLUGIN_CORE . 'inc/plugin_model.php';
            require_once get_path('plugin/lottery/core/model.php');
            $user_lt_flag_total = \LotteryModel::getUserFlags($themeid, $fans['uid']);
            if ($user_lt_flag_total < 1) {
                $component['is_lottery'] = false;
                $component['yx_lt_show'] = 2;
            }
        }
//判断是否开启拆红包
        if ($system['fenxiao'] == 1 && $config['iscomm'] == 1 && ($config['hbopenfee2'] || $config['hborderfee2'] || $config['ismycomm_old'] == 2 || $config['ismycomm_old'] == 3)) {
            $component['split_redpackage'] = true;
        }
        $into_no_hb = false;
        if (($config['hbopenfee1'] == 0 && $config['hbopenfee2'] == 0) && ($config['hborderfee1'] > 0 || $config['hborderfee2'] > 0)) {
            $into_no_hb = true;
        }
        $component['into_no_hb'] = $into_no_hb;

        if ($system['iskoi'] == 1 || $system['is_lz_game'] == 1) {
            $component['lz_game_img'] = $config['lz_game_img']; //拼团首页【进入游戏】Banner图
            $component['koi_five_location'] = $config['koi_five_location'];
            $component['lz_game_img_position'] = $config['lz_game_img_position']; //拼团首页【进入游戏】显示位置
            $component['enter_lz_game_icon'] = $config['enter_lz_game_icon'];
            $component['koi_top_img'] = $config['koi_top_img'];

            $five_theme = Game::getThemeListByType('five_koi', $themeid);
            if ($five_theme['type_theme_total'] > 0) {
                $new_theme = Theme::getThemeById($five_theme['type_theme_list'][0]['id']);
                $fanstemp = Fans::getworkerid_fromuid($themeid, $fans);
                $workerid = $fanstemp['workerid'];
                $fromuid = $fanstemp['fromuid'];
                $xcxurl = '/pages/index/index?softname=' . MODULE_NAME . '&themeid=' . $new_theme['id'] . '&softkey=' . $new_theme['softkey'] . '&fromuid=' . $fromuid . '&workerid=' . $workerid;
                $component['koi_page_path'] = $xcxurl;
                $component['enter_koi_icon'] = $config['pintuan_to_koi_icon'];
            }
        }
//判断用户是否存在摇一摇礼品
        if ($system['fenxiao071']) {
            $sql = " select count(*) as cnt from " . tablename(t_yyy_gifts_flag) . " where uid={$fans['uid']} and themeid=$themeid";
            $fans_gift_total = db_find($sql)['cnt'];
            if ($fans_gift_total > 0) {
                $component['yx_gift_icon'] = $config['yx_gift_icon'];
            }
        }
//夺宝
        if ($system['invite_gift'] == 1) {
            $gift_config = db_get(t_intive_gift_config, array('software' => MODULE_NAME, 'themeid' => $themeid), array('show_mode'));
            $component['gift_show_mode'] = $gift_config['show_mode'];
        }
        return $component;
    }

    protected function baseinfo()
    {
        global $_P, $_G;
        $fans = self::checkuser();
        $system = $_G['system'];
        $themeid = intval($_P['themeid']);
        $theme = Theme::getThemeByIdxcx($themeid);
        if (empty($theme['id'])) {
            wxapp_result(1, '活动入口不正确');
            exit;
        }
        $config = Theme::getConfig($themeid);

        $jubf['title'] = empty($theme['jubf']) ? '' : $theme['jubf'] . '-';
        $jubf['banquan'] = !empty($theme['USERCOMPANYNAME']) ? $theme['USERCOMPANYNAME'] : $_G['companyname'];
        $fanstemp = Fans::getworkerid_fromuid($themeid, $fans);
        $result = array(
            'system' => $system,
            'theme' => $theme,
            'config' => $config,
            'fans' => $fans,
            'themeid' => $themeid,
            'workerid' => $fanstemp['workerid'],
            'fromuid' => $fanstemp['fromuid'],
            'jubf' => $jubf,
            'jishu' => Worker::getjishu($theme, $system),
        );
        return $result;
    }

    protected function pay($params)
    {
        global $_G, $_P;
        if ($_G['app_web_wxapp'] != 'wxapp') {
            return ['errno' => 1, 'message' => '支付功能只能在微信小程序中使用'];
        }
        $params['module'] = $params['module_name'] = MODULE_NAME;
        $log = Db::table('yx_sys_paylog')->where(array('mpid' => $_G['mpid'], 'module' => $params['module'], 'tid' => $params['tid']))->find();
        if (empty($log)) {
            $log = [
                'mpid' => $_G['mpid'],
                'openid' => $_G['member']['openid'],
                'module' => MODULE_NAME,
                'tid' => $params['tid'],
                'uniontid' => $params['uniontid'],
                'fee' => $params['fee'],
                'status' => 0,
            ];
            $log['id'] = Db::table('yx_sys_paylog')->insertGetId($log);
        }
        if (!empty($log) && $log['status'] == '1') {
            return ['errno' => 1, 'message' => '这个订单已经支付成功, 不需要重复支付.'];
        }
        $params['openid'] = $log['openid'];
        if (!empty($log['id'])) {
            $tag = [];
            $tag['mpid'] = $_G['mpid'];
            $tag['uid'] = $_G['member']['uid'];
            db_update('yx_sys_paylog', ['openid' => $_G['member']['openid'], 'tag' => xserializer($tag)], ['id' => $log['id']]);
        }
        $platform_item = db_get('yx_sys_platform', ['id' => $log['mpid']]);
        $module = Db::table('yx_sys_modules')->where(['module_name' => $log['module'], 'themeid' => $params['themeid']])->find();
        if ($module) {
            $_G['system'] = $module['system'] = empty($module['system']) ? array() : json_decode($module['system'], true);
            $_G['theme'] = $module['theme'] = empty($module['theme']) ? array() : json_decode($module['theme'], true);
        }
        if ($platform_item && $platform_item['mchid'] && $platform_item['apikey']) {
            $wechat['appid'] = $platform_item['appid'];
            $wechat['mchid'] = $platform_item['mchid'];
            $wechat['apikey'] = $wechat['signkey'] = $platform_item['apikey'];
            //判断与system不一致拦截
            if ($wechat['mchid'] != $_G['system']['mchidxcx']) {
                return ['errno' => 1, 'message' => '参数设置商户信息与平台设置信息不一致'];
            }
        } else {
            if (!$module) {
                return ['errno' => 1, 'message' => '参数设置商户信息与平台设置信息不一致'];
            }
            $wechat['appid'] = $module['system']['appidxcx'];
            $wechat['mchid'] = $module['system']['mchidxcx'];
            $wechat['apikey'] = $wechat['signkey'] = $module['system']['apikeyxcx'];
        }

        $pre_pay_res = PaymentModel::unifiedorder($params, $wechat);
        if ($pre_pay_res['errcode'] == 1) {
            if ($pre_pay_res['msg'] == 'invalid out_trade_no' || $pre_pay_res['msg'] == 'OUT_TRADE_NO_USED') {
                $tmp_id = date('YmdH');
                Db::table('yx_sys_paylog')->where(['id' => $log['id']])->update(['id' => $tmp_id]);
                Db::query("ALTER TABLE `yx_sys_paylog` auto_increment = " . ($tmp_id + 1) . ";");
                return ['errno' => 1, 'message' => '抱歉，发起支付失败，具体原因为：' . $pre_pay_res['errcode'] . ':' . $pre_pay_res['msg'] . '。请及时联系站点管理员。'];
            } else {
                return ['errno' => 1, 'message' => $pre_pay_res['msg']];
            }
        }
        return $pre_pay_res['data'];
    }
}

